<template>
  <div>
    商品分类

 <!-- 添加 -->
    <el-card class="box-card">
      <span><i class="iconfont icon-dingdan1 icon"> </i>产品分类数据列表</span>
      <el-button
        style="float: right; padding: 3px"
        type="primary"
        @click="goCateAddPage"
        >添加</el-button
      >
      <el-button
        style="float: right; padding: 3px; margin-right: 5px"
        type="primary"
        v-if="parentId != 0"
        @click="goBackLevel"
        >返回上级节点</el-button
      >
    </el-card>

    <el-table :data="productCateList" border style="width: 100%;margin:10px">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column prop="level" label="级别">
        <template slot-scope="{ row }">
          <span>{{ row.level === 0 ? "一级分类" : "二级分类" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="product_count" label="商品数量"> </el-table-column>
      <el-table-column prop="product_unit" label="数量单位"> </el-table-column>
      <el-table-column label="是否显示" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.show_status"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column label="设置"> 
         <template slot-scope="scope">
           <el-button
            type="primary"
            size="mini"
            @click="showNextLevel(scope.row.id)"
            :disabled="scope.row.level == 1"
            >查看下级</el-button
          >
          </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template>
          <el-button type="primary" size="mini">编辑</el-button>
          <el-button type="primary" size="mini" class="btn">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 1.分页 
    current-page 当前页码
    page-sizes   每页有多少条数据的下拉列表
    page-size    当前的列表每页有多少条数据
    total        数据的总数量
    size-change   修改每页有多少条数据
    current-change 修改当前是第几页

    4.分页：修改了current-page、page-sizes page-size total属性绑定的值
    -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
       style="float: right"
    >
    </el-pagination>
  </div>
</template>

<script>
//引入

import { fetchProductCatePage } from "@/api/product";
export default {
  name: "productCate",
  data: function () {
    return {
      cateList: [],
      //2.分页：定义变量
      listQuery: {
        pageSize: 5, //一页5条数据
        pageNum: 1, //当前页
      },
      total: 0, //总数量
       parentId: 0, //父级id号  查看下一级和返回上一级
    
    };
  },
  methods: {

     
     
    async getCateList() {
      const res = await fetchProductCatePage(this.listQuery, this.parentId);
      console.log(res);
      this.productCateList = res.data;
      //3.分页：获取数据的总量
       this.total = res.total;
    },
     handleSizeChange(val) {
        console.log("修改每页显示的条数 触发了",val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      //重新获取数据
      this.getCateList();
      },
      //6.分页：修改当前页的页码
      handleCurrentChange(val) {
        console.log("修改当前页的页码 触发了", val);
         this.listQuery.pageNum = val;
      this.getCateList();
      },
      //查看下一级
    showNextLevel(id) {
      this.parentId = id;
      this.getCateList();
    },
    goBackLevel() {
      this.parentId = 0;
      this.getCateList();
    },
     //跳转到添加页面
    goCateAddPage() {
      this.$router.push("/pms/productCateAdd");
    },
  },
  mounted() {
    this.getCateList();
  },
};
</script>

<style>
.btn {
    color: #fff;
    background-color: hsl(2, 100%, 63%);
    border-color: hsl(2, 100%, 63%);
  }
</style>